﻿using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using YAPACS.Organizations;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.Report;

public class GetCriticalValue(ICurrentUserOrganization currentUserOrganization,
    IRepository<ApplicationUser, Guid> userRepoitory) : WorkflowBase, IGetCriticalValue, ITransientDependency
{
    private readonly ICurrentUserOrganization _currentUserOrganization = currentUserOrganization;
    private readonly IRepository<ApplicationUser, Guid> _userRepoitory = userRepoitory;

    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        var workflowOutput = new WorkflowOutput
        {
            Success = true,
            NextActionCaption = "通知",
            ViewTitle = "危急值"
        };
        const string key = "Report";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is BusinessObjects.Exams.Report report)
        {
            var user = await _userRepoitory.GetAsync(_currentUserOrganization.UserId);
            var exam = report.Exam;
            bool readOnly = true;
            if (report.InitialWritingDoctor == user || report.ReportDoctor == user || report.AuditDoctor == user || report.ReviewDoctor == user)
            {
                workflowOutput.NextAction = "报告-设置危急值";
                readOnly = false;
            }
            //查询数据
            workflowOutput.Data = new
            {
                ReportId = report.Oid,
                exam.Patient?.Name,
                exam.Age,
                Sex = (int?)exam.Patient?.Sex,
                exam.Patient?.PhoneNumber,
                exam.OutPatientNumber,
                exam.PhysicalExamNumber,
                exam.InPatientNumber,
                exam.BedNumber,
                exam.InPatientArea,
                exam.WardNumber,
                ApplyDepartment = exam.ApplyDepartment?.Name,
                ApplyDepartmentId = exam.ApplyDepartment?.Oid,
                ApplyDoctor = exam.ApplyDoctor?.Name,
                ApplyDoctorId = exam.ApplyDoctor?.Oid,
                SendingTime = report.ReportPanicValue?.SendingTime?.ToString("yyyy-MM-dd HH:mm:ss"),
                exam.ClinicalDiagnosis,
                ExamItems = string.Join(", ", exam.ExamItems.Select(x => x.Name).ToList()),
                ReceivingTime = report.ReportPanicValue?.ReceivingTime?.ToString("yyyy-MM-dd HH:mm:ss"),
                SendingPhoneNumber = report.ReportPanicValue != null ? report.ReportPanicValue.SendingPhoneNumber : user.PhoneNumber,
                report.ReportPanicValue?.Result,
                report.ReportPanicValue?.Description,
                PanicValueId = report.ReportPanicValue?.Oid,
                report.ReportPanicValue?.ReceivingPhoneNumber,
                Readonly = readOnly
            };
        }
        workflowOutput.View = await Extensions.GetResourceStringAsync("Resources.Report.critical-values.json");
        return workflowOutput;
    }
}
